﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using RabbitMQCommon;

namespace RabbitMQCommon.SimpleMode
{
    public class ConsumerMessage
    {
        public static void ReceivedMessage()
        {
            //打开连接
            using (var connection = FactoryConnecation.factory.CreateConnection())
            {
                //打开通道
                using (var channel = connection.CreateModel())
                {
                    //3.队列声明   参数:队列名 持久性:value(是持久/非持久) 是否独家的   自动删除  
                    channel.QueueDeclare(queue: FactoryConnecation.QueueName,
                                 durable: false,
                                 exclusive: false,
                                 autoDelete: false,
                                 arguments: null);

                    
                    var consumer = new EventingBasicConsumer(channel);
                    //consumer.Received += Consumer_Received;
                    consumer.Received += (model, ea) => 
                    {
                        var body = ea.Body.ToArray();
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine($"接收消息:{message}");
                    };
                    channel.BasicConsume(FactoryConnecation.QueueName, true, consumer);
                    Console.ReadLine();
                }
            }
        }
    }
}
